package com.cn.qgx428.lazydiary.ui.activity.subActivity

import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.cn.qgx428.lazydiary.Presenter.UserPresenter
import com.cn.qgx428.lazydiary.R
import com.cn.qgx428.lazydiary.Utils.FileUtil
import com.cn.qgx428.lazydiary.network.Imageloader
import com.cn.qgx428.lazydiary.Utils.PhotoChoseActivity
import com.cn.qgx428.lazydiary.local.LAMessege
import com.cn.qgx428.lazydiary.local.LAction
import com.cn.qgx428.lazydiary.local.LActionWacher
import com.cn.qgx428.lazydiary.local.LManager
import com.cn.qgx428.lazydiary.network.bean.User
import com.cn.qgx428.lazydiary.ui.activity.BaseActivity

import kotlinx.android.synthetic.main.activity_user_info.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

class UserInfoActivity : BaseActivity(), LActionWacher {

    var userId = -1;
    var userInfo: User = User(0,"","","","","")
    var checkBox = mutableListOf<TextView>()
    var currentGender = "保密"
    val unselectTextColor = R.color.dimGray
    val selectTextColor = R.color.mBlack

    // 用户数据更新存储
    var imgPath = ""

    override fun initUI() {
        hindActionBar()
        setStatuBarColor(Color.BLACK)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user_info)
        getUserInfo()
    }

    /**
     * 页面事件延迟到获取用户数据，有需要再改动
     */
    private fun initView() {
        // 获取gender控件
        for (i in 0..userInfo_genderCheckBox.childCount){
            val view  = userInfo_genderCheckBox.getChildAt(i)
            if (view is TextView){
                checkBox.add(view)
            }
        }
        // 设置选择监听
        checkBox.forEach {
            it.setOnClickListener {
                currentGender = (it as TextView).text.toString()
                changeGenderSelect(it)
            }
        }

        userInfo_img.setOnClickListener({
            startActivity<PhotoChoseActivity>()
            overridePendingTransition(R.anim.from_center_appear_in, R.anim.keep)
            LManager.newInstance().register(this,LAction.ACTION_GET_LOCAL_IMAGE)
        })

        userInfo_commit.setOnClickListener({
            UserPresenter.updateUserInfo(userId,userInfo_nickname.text.toString(),currentGender,
                    FileUtil.getFileName(imgPath),imgPath, {
                toast(it)
                // 更新完后结束页面，同时其他页面用户信息已更新
                val msg = LAMessege()
                msg.argObject = it
                LManager.newInstance().trigger(LAction.ACTION_UPDATE_USER_INFO, msg)
                finish()
            })
        })

        userInfo_bar.setBackOnClickListener { finish() }

        userInfo_nickname.setText(userInfo.nickname)
        userInfo_phone.setText(userInfo.phone)
        Imageloader.download(this, userInfo_img , userInfo.image)
    }

    private fun changeGenderSelect(it: View) {
        checkBox.forEach {
            if (it.text.equals(currentGender)){
                it.setTextColor(selectTextColor)
                it.setBackgroundResource(R.drawable.activity_userinfo_gender_select)
            }else{
                it.setTextColor(unselectTextColor)
                it.setBackgroundResource(R.drawable.activity_userinfo_gender_unselect)
            }
        }
    }


    private fun getUserInfo() {
        userId = intent.extras.getString("userId").toInt()
        UserPresenter.getUserInfo(userId,{
            userInfo = it
            currentGender = userInfo.gender
            initView()
        })
    }



    /**
     * 本地监听
     */
    override fun onTrigger(action: String, msg: LAMessege) {
        when(action){
            LAction.ACTION_GET_LOCAL_IMAGE->{
                val bitmap = BitmapFactory.decodeFile(msg.arg1)
                userInfo_img.setImageBitmap(bitmap)
                imgPath = msg.arg1
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        LManager.newInstance().unRegister(this)
    }
}
